package com.zrf.gulimall.search.controller;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author zhanrf
 * @Date 2021/12/26 19:04
 */
public class ThreadTest {
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		System.out.println("main执行开始");
		CompletableFuture<Integer> handle = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread().getName() + "执行开始");
			int i = 10 / 1;
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "执行结束");
			return i;
		}).handle((res, exception) -> {
			System.out.println(Thread.currentThread().getName() + "handler执行开始");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (Objects.nonNull(exception)) {
				return 0;
			}
			System.out.println(Thread.currentThread().getName() + "handler执行结束");
			return res;
		});
		System.out.println("main执行完成");
		Integer integer = handle.get();
	}
}
